﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UIListener : MonoBehaviour {

    public Button retryButton;
    public Text achievementStr;

    GameScore gameScore;

	// Use this for initialization
	void Start() {
        gameScore = GetComponent<GameScore>();
        retryButton.onClick.AddListener(RetryGame);
        TargetHolder.OnGameEnd += ShowRetryButton;
    }
	
    void RetryGame()
    {
        this.gameObject.SetActive(false);
        SceneManager.LoadScene(0);
    }

    void ShowRetryButton()
    {
        retryButton.gameObject.SetActive(true);
        ShowAchievement();
    }

    private void OnDestroy()
    {
        TargetHolder.OnGameEnd -= ShowRetryButton;
    }

    private void ShowAchievement()
    {
        float score = gameScore.GetScore();
        if(score < 3)
        {
            achievementStr.text = String.achievementStrings[0];
        }
        else if(score< 8)
        {
            achievementStr.text = String.achievementStrings[1];
        }
        else if(score < 12)
        {
            achievementStr.text = String.achievementStrings[2];
        }
        else if(score < 16)
        {
            achievementStr.text = String.achievementStrings[3];
        }
        else
        {
            achievementStr.text = String.achievementStrings[4];
        }
    }


}
